package com.springboot.service.impl;

import com.springboot.mapper.AssistantMessageInformMapper;
import com.springboot.mapper.MessageInformMapper;
import com.springboot.mapper.OrderFormMapper;
import com.springboot.mapper.PatientInfoFormMapper;
import com.springboot.model.AssistantMessageInform;
import com.springboot.model.AssistantMessageInformKey;
import com.springboot.model.MessageInform;
import com.springboot.model.OrderForm;
import com.springboot.service.AssistantMessageListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class AssistantMessageListServiceImpl implements AssistantMessageListService {
    @Autowired
    private AssistantMessageInformMapper assistantMessageInformMapper;
    @Autowired
    private MessageInformMapper messageInformMapper;
    @Autowired
    private OrderFormMapper orderFormMapper;
    @Autowired
    private PatientInfoFormMapper patientInfoFormMapper;

    @Override
    public ArrayList<AssistantMessageInform> getAssistantMessageInformById(Integer assistantId){
        ArrayList<AssistantMessageInform> assistantMessageInforms=new ArrayList<>();
        try{
            assistantMessageInforms=assistantMessageInformMapper.selectByAssistantId(assistantId);
        }
        catch (Exception e){
            System.out.println("assistantMessageInformMapper出错");
            e.printStackTrace();
        }
        return assistantMessageInforms;
    }

    @Override
    public int updateAssistantMessageInform(AssistantMessageInformKey assistantMessageInformKey){
        int a=0;
        try{
            a=assistantMessageInformMapper.updateByTwoKey(assistantMessageInformKey);
        }
        catch (Exception e){
            System.out.println("assistantMessageInformMapper更新出错");
            e.printStackTrace();
        }
        return  a;
    }

    @Override
    public MessageInform getMessageInformById(Integer messageId){
        MessageInform messageInform=new MessageInform();
        try{
            messageInform=messageInformMapper.selectByPrimaryKey(messageId);
        }
        catch (Exception e){
            System.out.println("messageInformMapper出错");
            e.printStackTrace();
        }
        return messageInform;
    }

    @Override
    public AssistantMessageInform getAssistantMessageInformBy(AssistantMessageInformKey assistantMessageInformKey){
        AssistantMessageInform assistantMessageInform=new AssistantMessageInform();
        try{
            assistantMessageInform=assistantMessageInformMapper.selectByPrimaryKey(assistantMessageInformKey);
        }
        catch (Exception e){
            System.out.println("assistantMessageInformMapper出错");
        }
        return assistantMessageInform;
    }

    @Override
    public int insertCancelMessageInform(MessageInform messageInform){
        int a=-1;
        try{
            a=messageInformMapper.insertSelective(messageInform);
            //MessageInform messageInform1=messageInformMapper.selectByMessageInform(messageInform);
            //a=messageInform1.getMessageId();
        }
        catch (Exception e){
            System.out.println("messageInformMapper错误");
            e.printStackTrace();
        }
        return a;
    }

    @Override
    public int insertCancelAssistentMessageInform(AssistantMessageInform assistantMessageInform){
        int a=-1;
        try{
            assistantMessageInformMapper.insertSelective(assistantMessageInform);
        }
        catch (Exception e){
            System.out.println("assistantMessageInformMapper错误");
            e.printStackTrace();
        }
        return a;
    }

    @Override
    public int deleteOrderForm(OrderForm orderForm){
        int a=-1;
        try {
            OrderForm orderForm1=orderFormMapper.selectByFourAttribute(orderForm);
            a=orderForm1.getBootId();
            int b=orderFormMapper.deleteByPrimaryKey(a);
            int c=patientInfoFormMapper.deleteByPrimaryKey(a);
        }
        catch (Exception e){
            System.out.println("orderFormMapper出错");
            e.printStackTrace();
        }
        return a;
    }

    @Override
    public int getMessageId(){
        int a=0;
        try {
            ArrayList<MessageInform> messageInforms=messageInformMapper.getAllRecord();
            int size=messageInforms.size();
            for (int i=0;i<size;i++){
                if(messageInforms.get(i).getMessageId()>a) {
                    a=messageInforms.get(i).getMessageId();
                }
            }
        }
        catch (Exception e){
            System.out.println("messageInformMapper找到最大MessageId");
        }

        return a;
    }

    @Override
    public OrderForm getIt(OrderForm orderForm){
        OrderForm orderForm1=new OrderForm();
        try {
            orderForm1=orderFormMapper.selectByFourAttribute(orderForm);
        }
        catch (Exception e){
            System.out.println("orderFormMapper出错");
            e.printStackTrace();
        }
        return orderForm1;
    }

    @Override
    public int getNoReadMessageNum(Integer assistant){
        int a=-1;
        try {
            a=assistantMessageInformMapper.getNoReadByAssistantId(assistant);
        }
        catch (Exception e){
            System.out.println("assistantMessageInformMapper出错");
            e.printStackTrace();
        }
        finally {
            return a;
        }
    }
}
